using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The EnquiryReasonTypeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about EnquiryReasonTypeTable objects.
    /// </summary>
    public class EnquiryReasonTypeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of EnquiryReasonTypeTable from the underlying datasource.
        /// </summary>
        /// <param name="enquiryReasonType">The unique EnquiryReasonType of the EnquiryReasonTypeTable in the database.</param>
        /// <returns>An EnquiryReasonTypeTable when the EnquiryReasonType was found in the database, or null otherwise.</returns>
        public static EnquiryReasonTypeTable GetItem(short enquiryReasonType)
        {
            EnquiryReasonTypeTable myEnquiryReasonTypeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryReasonTypeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@enquiryReasonType", enquiryReasonType);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myEnquiryReasonTypeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myEnquiryReasonTypeTable;
            }
        }

        /// <summary>
        /// Returns a list with EnquiryReasonTypeTable objects.
        /// </summary>
        /// <returns>A generics List with the EnquiryReasonTypeTable objects.</returns>
        public static EnquiryReasonTypeTableList GetList()
        {
            EnquiryReasonTypeTableList tempList = new EnquiryReasonTypeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryReasonTypeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a EnquiryReasonTypeTable in the database.
        /// </summary>
        /// <param name="myEnquiryReasonTypeTable">The EnquiryReasonTypeTable instance to save.</param>
        /// <returns>The new EnquiryReasonType if the EnquiryReasonTypeTable is new in the database or the existing EnquiryReasonType when an item was updated.</returns>
        public static Int16 Save(EnquiryReasonTypeTable myEnquiryReasonTypeTable)
        {
            Int16 result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryReasonTypeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myEnquiryReasonTypeTable.EnquiryReasonType == -1)
                    {
                        myCommand.Parameters.AddWithValue("@enquiryReasonType", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@enquiryReasonType", myEnquiryReasonTypeTable.EnquiryReasonType);
                    }
                    if (string.IsNullOrEmpty(myEnquiryReasonTypeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myEnquiryReasonTypeTable.Description);
                    }
                    if (myEnquiryReasonTypeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myEnquiryReasonTypeTable.Status);
                    }
                    if (myEnquiryReasonTypeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myEnquiryReasonTypeTable.CreatedDateTime);
                    }
                    if (myEnquiryReasonTypeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myEnquiryReasonTypeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a EnquiryReasonTypeTable from the database.
        /// </summary>
        /// <param name="enquiryReasonType">The EnquiryReasonType of the EnquiryReasonTypeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short enquiryReasonType)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryReasonTypeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@enquiryReasonType", enquiryReasonType);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the EnquiryReasonTypeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static EnquiryReasonTypeTable FillDataRecord(IDataRecord myDataRecord)
        {
            EnquiryReasonTypeTable myEnquiryReasonTypeTable = new EnquiryReasonTypeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EnquiryReasonType")))
            {
                myEnquiryReasonTypeTable.EnquiryReasonType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("EnquiryReasonType"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myEnquiryReasonTypeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myEnquiryReasonTypeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myEnquiryReasonTypeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myEnquiryReasonTypeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myEnquiryReasonTypeTable;
        }
    }

}
